package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.dao.CategoryDao;
import com.itheima.dao.SetmealDao;
import com.itheima.dao.SetmealDishDao;
import com.itheima.pojo.*;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealDishDao setmealDishDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    @CacheEvict(value = "setmealCache",key = "'setmeal_'+#setmealDto.categoryId")
    public void saveSetmeal(SetmealDto setmealDto) {
//        //添加套餐数据
//        setmealDao.insert(setmealDto);
//        //添加套餐菜品数据
//        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
//        if (setmealDishes != null && setmealDishes.size() > 0 ){
//            Long setmealId = setmealDto.getId();
//            for (SetmealDish setmealDish : setmealDishes) {
//                setmealDish.setSetmealId(setmealId);
//                setmealDishDao.insert(setmealDish);
//            }
//        }
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page<SetmealDto> pageSelect(Integer page, Integer pageSize, String name) {
        //构造分页构造器对象
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //添加过滤条件
        lqw.like(name != null && !"".equals(name),Setmeal::getName,name);
        //执行分页查询
        setmealDao.selectPage(setmealPage,lqw);
        //给setmealDtoPage的total属性赋值
        setmealDtoPage.setTotal(setmealPage.getTotal());
        List<Setmeal> setmeals = setmealPage.getRecords();
        List<SetmealDto> setmealDtos = new ArrayList<>();
        for (Setmeal setmeal : setmeals) {
            SetmealDto setmealDto = new SetmealDto();
            Category category = categoryDao.selectById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            //对象拷贝
            BeanUtils.copyProperties(setmeal,setmealDto);
            setmealDtos.add(setmealDto);
        }
        setmealDtoPage.setRecords(setmealDtos);
        return setmealDtoPage;
    }

    @Override
    public R deleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getStatus,1);
        lqw.in(Setmeal::getId,ids);
        Integer count = setmealDao.selectCount(lqw);
        if (count > 0){
            return R.error("套餐在售，无法删除");
        }
        setmealDao.deleteBatchIds(ids);
        return R.success("删除成功");
    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> luw = new LambdaUpdateWrapper<>();
        luw.set(Setmeal::getStatus,status);
        luw.in(Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmealDao.update(setmeal,luw);
    }

    @Override
    public SetmealDto getSetmealDto(Long id) {
        //查询套餐
        Setmeal setmeal = setmealDao.selectById(id);
        //查询套餐菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishDao.selectList(lqw);
        //封装SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    @Override
    @Cacheable(value = "setmealCache",key = "'setmeal_'+#categoryId",unless = "#result == null")
    public List<Setmeal> selectSetmeal(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,categoryId);
        lqw.eq(Setmeal::getStatus,1);
        List<Setmeal> setmeals = setmealDao.selectList(lqw);
        return setmeals;
    }

    @Override
    public List<SetmealDish> selectSetmealDish(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> setmealDishes = setmealDishDao.selectList(lqw);
        return setmealDishes;
    }

    @Override
    public void updateSetmealDto(SetmealDto setmealDto) {
        //修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal);
        setmealDao.updateById(setmeal);
        //修改套餐菜品
        Long setmealId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        //先删除原有菜品口味数据
        setmealDishDao.delete(lqw);
        //再存菜品口味数据
        //获取修改的套餐菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //遍历添加
        if (setmealDishes != null && setmealDishes.size() > 0){
            for (SetmealDish setmealDish : setmealDishes) {
                //添加套餐id
                setmealDish.setSetmealId(setmealId);
                setmealDishDao.insert(setmealDish);
            }
        }
    }
}
